package com.jrx.anytxn.reporting.job.reportingFile.config;

import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.reporting.config.AnyTxnBatchProperties;
import com.jrx.anytxn.reporting.constant.ReportingConstant;
import com.jrx.anytxn.reporting.entity.MrRepaymentPlan;
import com.jrx.anytxn.reporting.job.reportingFile.step.RepaymentPlanFileListener;
import com.jrx.anytxn.reporting.job.reportingFile.step.RepaymentPlanFileProcessor;
import com.jrx.anytxn.reporting.job.reportingFile.step.RepaymentPlanFileReader;
import com.jrx.anytxn.reporting.job.reportingFile.step.RepaymentPlanFileWriter;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisPagingItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import javax.annotation.Resource;

@Configuration
@EnableConfigurationProperties(AnyTxnBatchProperties.class)
@EnableBatchProcessing
public class GeneratePlanFileConfiguration {

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private AnyTxnBatchProperties anyTxnBatchProperties;
    @Autowired
    private IOrganizationTableService organizationTableService;
    @Autowired
    private RepaymentPlanFileProcessor repaymentPlanFileProcessor;
    @Autowired
    private RepaymentPlanFileWriter repaymentPlanFileWriter;
    @Autowired
    private RepaymentPlanFileListener repaymentPlanFileListener;


    @Bean
    public Job generatePlanFileJob() {
        return jobBuilderFactory.get("generatePlanFileJob").incrementer(new RunIdIncrementer()).listener(repaymentPlanFileListener)
                .start(generatePlanFileStep())
                .build();
    }

    @Bean
    public Step generatePlanFileStep(){
        return stepBuilderFactory.get("generatePlanFileStep")
                .<MrRepaymentPlan, String>chunk(anyTxnBatchProperties.getFileConfig().getChunkLimit())
                //表示一批处理100
                // 条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(repaymentPlanFileReader())
                .processor(repaymentPlanFileProcessor)
                .writer(repaymentPlanFileWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anyTxnBatchProperties.getFileConfig().getThrottleLimit())//多线程*/
                .build();
    }

    @Bean
    @StepScope
    public MyBatisPagingItemReader<MrRepaymentPlan> repaymentPlanFileReader() {
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(ReportingConstant.DEFAULT_ORG_NUMBER);
        return new RepaymentPlanFileReader(sqlSessionFactory, org,anyTxnBatchProperties.getFileConfig().getPageSize());
    }

}
